package org.gary.core.util;

import java.sql.ResultSet;
import java.sql.SQLException;

public class Util {
	private int ye; // 当前页
	private int hang; // 每页行数
	private int zys; // 总页数
	private int zhs; // 总行数
	private int cid;

	private DBUtile dbUtil=new DBUtile();

	/**
	 * 得到总行数
	 * @return
	 */
	public Util(String from, int h, Object lang) {
		this.hang = h;
		String sql = "Select count(*) from " + from + " where visible=1 and lang = '" + lang + "'";
		ResultSet rs = null;
		try {
			rs = dbUtil.execuetQuery(sql, null);
			while (rs.next()) {
				zhs = rs.getInt(1); // 得到总行数
				if (zhs % hang == 0) {
					zys = zhs / hang;
				} else if (zhs % hang != 0) {
					zys = zhs / hang + 1;
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			if (rs != null) {
				try {
					rs.close();
				} catch (SQLException e) {
					e.printStackTrace();
				}
			}
			dbUtil.closedAll();
		}
	}

	public Util(String from, int h, int cid, Object lang) {
		this.hang = h;
		String sql = "Select count(*) from " + from
				+ " where visible=1 and category_id=" + cid
				+ " and lang = '" + lang + "'";
		ResultSet rs = null;
		try {
			rs = dbUtil.execuetQuery(sql, null);
			while (rs.next()) {
				zhs = rs.getInt(1); // 得到总行数

				if (zhs % hang == 0) {
					zys = zhs / hang;
				} else if (zhs % hang != 0) {
					zys = zhs / hang + 1;
				}
			}
		} catch (Exception e) {

			e.printStackTrace();
		} finally {
			if (rs != null) {
				try {
					this.cid = cid;
					rs.close();
				} catch (SQLException e) {
					e.printStackTrace();
				}
			}
			dbUtil.closedAll();
		}
	}

	public int getCid() {
		return cid;
	}

	public void setCid(int cid) {
		this.cid = cid;
	}

	/**
	 * 得到当前页的前面第五页的页码
	 * 
	 * @return
	 */
	public int getBegin() {
		int begin = 1;
		if (ye > 5) {
			begin = ye - 5;
		} else {
			begin = 1;
		}
		return begin;
	}

	/**
	 * 得到当前也的后面第五页的页码
	 */
	public int getEnd() {
		int end = 1;
		if (ye + 5 > zys) {
			end = zys;
		} else {
			end = ye + 5;
		}
		return end;
	}

	/**
	 * 得到上一页页码
	 */
	public int getUpPage() {
		int end = 1;
		if (ye - 1 < 1) {
			end = 1;
		} else {
			end = ye - 1;
		}
		return end;
	}

	/**
	 * 得到下一页页码
	 */
	public int getNextPage() {
		int end = 1;
		if (ye + 1 > zys) {
			end = zys;
		} else {
			end = ye + 1;
		}
		return end;
	}

	public int getYe() {
		return ye;
	}

	public void setYe(int ye) {
		this.ye = ye;
	}

	public int getHang() {
		return hang;
	}

	public void setHang(int hang) {
		this.hang = hang;
	}

	public int getZys() {
		return zys;
	}

	public void setZys(int zys) {
		this.zys = zys;
	}

	public int getZhs() {
		return zhs;
	}

	public void setZhs(int zhs) {
		this.zhs = zhs;
	}
}
